package cn.tedu.user.management.dao.persist.repository.impl;

import cn.tedu.rtms.commons.pojo.vo.PageData;
import cn.tedu.rtms.commons.util.PageInfoToPageDataConverter;
import cn.tedu.user.management.dao.persist.mapper.RentMapper;
import cn.tedu.user.management.dao.persist.mapper.UserMapper;
import cn.tedu.user.management.dao.persist.mapper.UserRoleMapper;
import cn.tedu.user.management.dao.persist.repository.IUserRepository;
import cn.tedu.user.management.pojo.entity.User;
import cn.tedu.user.management.pojo.vo.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 处理用户数据的数据访问实现类
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Slf4j
@Repository
public class UserRepositoryImpl implements IUserRepository {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RentMapper rentMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    public UserRepositoryImpl() {
        log.debug("创建数据访问实现类对象：UserRepositoryImpl");
    }

    @Override
    public int insert(User user) {
        log.debug("开始执行【插入用户】的数据访问，参数：{}", user);
        return userMapper.insert(user);
    }
    /*
    @Override
    public int insertBatch(List<User> userList) {
        log.debug("开始执行【批量插入用户】的数据访问，参数：{}", userList);
        return userMapper.insertBatch(userList);
    }*/

    @Override
    public int deleteById(Long id) {
        log.debug("开始执行【根据ID删除用户】的数据访问，参数：{}", id);
        return userMapper.deleteById(id);
    }

    /*@Override
    public int deleteByIds(Collection<Long> idList) {
        log.debug("开始执行【批量删除用户】的数据访问，参数：{}", idList);
        return userMapper.deleteBatchIds(idList);
    }*/

    @Override
    public int updateById(User user) {
        log.debug("开始执行【更新用户】的数据访问，参数：{}", user);
        return userMapper.updateById(user);
    }

    /*@Override
    public int count() {
        log.debug("开始执行【统计用户的数量】的数据访问，参数：无");
        return userMapper.selectCount(null);
    }*/

    @Override
    public int countByUsername(String username) {
        log.debug("开始执行【根据用户名统计用户的数量】的数据访问，参数：{}", username);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public int countByRealName(String realName) {
        log.debug("开始执行【根据用户名统计姓名的数量】的数据访问，参数：{}", realName);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("real_name", realName);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public int countByPhone(String phone) {
        log.debug("开始执行【根据手机号码统计用户的数量】的数据访问，参数：{}", phone);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public int countByEmail(String email) {
        log.debug("开始执行【根据电子邮箱统计用户的数量】的数据访问，参数：{}", email);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", email);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public UserStandardVO getStandardById(Long id) {
        log.debug("开始执行【根据ID查询用户详情】的数据访问，参数：{}", id);
        return userMapper.getStandardById(id);
    }

    @Override
    public UserLoginInfoVO getLoginInfoByUsername(String username) {
        log.debug("开始执行【根据用户名查询用户登录信息】的数据访问，参数：{}", username);
        return userMapper.getLoginInfoByUsername(username);
    }

    @Override
    public PageData<UserListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询用户列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<UserListItemVO> list = userMapper.list();
        PageInfo<UserListItemVO> pageInfo = new PageInfo<>(list);
        PageData<UserListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public PageData<UserListItemVO> regList(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询注册列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<UserListItemVO> list = userMapper.regList();
        PageInfo<UserListItemVO> pageInfo = new PageInfo<>(list);
        PageData<UserListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public List<UserListItemVO> regList() {
        log.debug("开始执行【查询添加列表】的数据访问");
        return userMapper.regList();
    }

    @Override
    public List<UserListItemVO> selectByKeywords(String keywords) {
        log.debug("开始执行【根据关键字查询员工列表】的数据访问");
        return userMapper.selectByKeywords(keywords);
    }

    @Override
    public int update(User user) {
        log.debug("开始执行【更新员工信息】的数据访问，参数：{}", user);
        return userMapper.updateById(user);
    }

    @Override
    public int countByUsernameAndNotId(Long id, String username) {
        log.debug("开始执行【统计不匹配ID但匹配用户名的员工数量】的数据访问，ID：{}，名称：{}", id, username);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username).ne("id", id);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public int countByPhoneAndNotId(Long id, String phone) {
        log.debug("开始执行【统计不匹配ID但匹配手机号的员工数量】的数据访问，ID：{}，手机号：{}", id, phone);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone).ne("id", id);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public int countByEmailAndNotId(Long id, String email) {
        log.debug("开始执行【统计不匹配ID但匹配电子邮箱的员工数量】的数据访问，ID：{}，电子邮箱：{}", id, email);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", email).ne("id", id);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public int countBySectionJobId(Long sectionJobId) {
        log.debug("开始执行【根据部门职位id查询当前职位人数】的数据访问，id：{}", sectionJobId);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("section_job_id", sectionJobId);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public Long selectIdByRealName(String realName) {
        log.debug("开始执行【根据姓名查id】的数据访问，realName：{}", realName);
        return userMapper.selectUserIdByRealName(realName);
    }

    @Override
    public UserOwnInfoVO getUserOwnInfoById(Long id) {
        log.debug("开始执行【查看当前账号信息】的数据访问，id：{}", id);
        return userMapper.selectUserOwnInfoById(id);
    }

    @Override
    public int countByUsernameAndRealName(String username, String realName) {
        log.debug("开始执行【根据id和姓名查数量】的数据访问，username：{},realName{}", username, realName);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username).eq("real_name",realName);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public Long getIdByUsername(String username) {
        log.debug("开始执行【根据用户名查id】的数据访问，username：{}", username);
        return userMapper.selectIdByUsername(username);
    }

    @Override
    public Integer getWeightById(Long id) {
        log.debug("开始执行【根据id查weight】的数据访问，id：{}", id);
        return userMapper.selectWeightById(id);
    }

    @Override
    public int countRentNoReturnById(Long id) {
        log.debug("开始执行【根据id查询工具未归还记录】的数据访问，id：{}", id);
        return rentMapper.countRentNoReturnById(id);
    }

    @Override
    public UserRoleStandardVO getUserRoleById(Long id) {
        log.debug("开始执行【根据ID查询员工角色】的数据访问，参数：{}", id);
        return userRoleMapper.getUserRoleById(id);
    }

    @Override
    public List<UserListVO> selectUserList() {
        log.debug("开始执行【查询员工列表】的数据访问");
        return userMapper.selectUserList();
    }

    @Override
    public int countById(Long id) {
        log.debug("开始执行【根据id查询数量】的数据访问,id:{}",id);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        return userMapper.selectCount(queryWrapper);
    }

}
